import _regeneratorRuntime from "@babel/runtime/regenerator";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
var _excluded = ["tenantId"],
    _excluded2 = ["databaseId"];
import request from 'utils/request';
import { CPAAS_PLATFORM } from 'utils/config';
import { parseParameters, isTenantRoleLevel, getCurrentOrganizationId } from 'utils/utils';

function apiPrefix() {
  return isTenantRoleLevel() ? "".concat(getCurrentOrganizationId(), "/databases") : 'databases';
}
/**
 * 查询数据库列表数据
 * @async
 * @function queryDatabaseList
 * @param {object} params - 查询条件
 * @param {!number} [params.page = 0] - 数据页码
 * @param {!number} [params.size = 10] - 分页大小
 * @returns {object} fetch Promise
 */


export function queryDatabaseList(_x) {
  return _queryDatabaseList.apply(this, arguments);
}
/**
 * 添加数据库信息
 * @async
 * @function createDatabase
 * @param {object} params - 请求参数
 * @param {!object} params.databaseCode - 数据源代码
 * @param {!string} params.databaseName - 数据源名称
 * @param {?number} params.datasourceName - 数据库名称
 * @param {!string} params.enabledFlag - 启用标记
 * @returns {object} fetch Promise
 */

function _queryDatabaseList() {
  _queryDatabaseList = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(params) {
    var tenantId, otherParams, param;
    return _regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            tenantId = params.tenantId, otherParams = _objectWithoutProperties(params, _excluded);
            param = parseParameters(otherParams);
            return _context.abrupt("return", request("".concat(CPAAS_PLATFORM, "/v1/").concat(apiPrefix()), {
              method: 'GET',
              query: _objectSpread(_objectSpread({}, param), {}, {
                tenantId: tenantId
              })
            }));

          case 3:
          case "end":
            return _context.stop();
        }
      }
    }, _callee);
  }));
  return _queryDatabaseList.apply(this, arguments);
}

export function createDatabase(_x2) {
  return _createDatabase.apply(this, arguments);
}
/**
 * 编辑数据库信息
 * @async
 * @function editDatabase
 * @param {object} params - 请求参数
 * @param {!object} params.databaseCode - 数据源代码
 * @param {!string} params.databaseName - 数据源名称
 * @param {?number} params.datasourceName - 数据库名称
 * @param {!string} params.enabledFlag - 启用标记
 * @param {!string} params.databaseId - databaseId
 * @param {!string} params.objectVersionNumber - 版本号
 * @returns {object} fetch Promise
 */

function _createDatabase() {
  _createDatabase = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(params) {
    return _regeneratorRuntime.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            return _context2.abrupt("return", request("".concat(CPAAS_PLATFORM, "/v1/").concat(apiPrefix()), {
              method: 'POST',
              body: params
            }));

          case 1:
          case "end":
            return _context2.stop();
        }
      }
    }, _callee2);
  }));
  return _createDatabase.apply(this, arguments);
}

export function editDatabase(_x3) {
  return _editDatabase.apply(this, arguments);
}
/**
 * 删除数据库信息
 * @async
 * @function deleteDatabase
 * @param {number} databaseId - 数据库Id
 * @returns {object} fetch Promise
 */

function _editDatabase() {
  _editDatabase = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(params) {
    return _regeneratorRuntime.wrap(function _callee3$(_context3) {
      while (1) {
        switch (_context3.prev = _context3.next) {
          case 0:
            return _context3.abrupt("return", request("".concat(CPAAS_PLATFORM, "/v1/").concat(apiPrefix()), {
              method: 'PUT',
              body: params
            }));

          case 1:
          case "end":
            return _context3.stop();
        }
      }
    }, _callee3);
  }));
  return _editDatabase.apply(this, arguments);
}

export function deleteDatabase(_x4) {
  return _deleteDatabase.apply(this, arguments);
}
/**
 * 查询租户
 * @async
 * @function queryTenantList
 * @param {object} params - 查询条件
 * @param {!number} [params.page = 0] - 数据页码
 * @param {!number} [params.size = 10] - 分页大小
 * @returns {object} fetch Promise
 */

function _deleteDatabase() {
  _deleteDatabase = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4(params) {
    return _regeneratorRuntime.wrap(function _callee4$(_context4) {
      while (1) {
        switch (_context4.prev = _context4.next) {
          case 0:
            return _context4.abrupt("return", request("".concat(CPAAS_PLATFORM, "/v1/").concat(apiPrefix()), {
              method: 'DELETE',
              body: params
            }));

          case 1:
          case "end":
            return _context4.stop();
        }
      }
    }, _callee4);
  }));
  return _deleteDatabase.apply(this, arguments);
}

export function queryTenantList(_x5) {
  return _queryTenantList.apply(this, arguments);
}
/**
 * 添加租户
 * @async
 * @function selectTenantOk
 * @param {object} params - 请求参数
 * @param {!object} params.databaseId - 数据库id
 * @param {!string} params.tenantId - 租户id
 * @returns {object} fetch Promise
 */

function _queryTenantList() {
  _queryTenantList = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee5(params) {
    var databaseId, otherParams, param;
    return _regeneratorRuntime.wrap(function _callee5$(_context5) {
      while (1) {
        switch (_context5.prev = _context5.next) {
          case 0:
            databaseId = params.databaseId, otherParams = _objectWithoutProperties(params, _excluded2);
            param = parseParameters(otherParams);
            return _context5.abrupt("return", request("".concat(CPAAS_PLATFORM, "/v1/database-tenant"), {
              method: 'GET',
              query: _objectSpread(_objectSpread({}, param), {}, {
                databaseId: databaseId
              })
            }));

          case 3:
          case "end":
            return _context5.stop();
        }
      }
    }, _callee5);
  }));
  return _queryTenantList.apply(this, arguments);
}

export function selectTenantOk(_x6) {
  return _selectTenantOk.apply(this, arguments);
}
/**
 * 删除租户
 * @async
 * @function handleDeleteTenant
 * @param {object} params,databaseId - 请求参数
 * @param {number} databaseTenantId - 数据库租户Id
 * @returns {object} fetch Promise
 */

function _selectTenantOk() {
  _selectTenantOk = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee6(params) {
    return _regeneratorRuntime.wrap(function _callee6$(_context6) {
      while (1) {
        switch (_context6.prev = _context6.next) {
          case 0:
            return _context6.abrupt("return", request("".concat(CPAAS_PLATFORM, "/v1/database-tenant"), {
              method: 'POST',
              body: params
            }));

          case 1:
          case "end":
            return _context6.stop();
        }
      }
    }, _callee6);
  }));
  return _selectTenantOk.apply(this, arguments);
}

export function handleDeleteTenant(_x7) {
  return _handleDeleteTenant.apply(this, arguments);
}

function _handleDeleteTenant() {
  _handleDeleteTenant = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee7(params) {
    return _regeneratorRuntime.wrap(function _callee7$(_context7) {
      while (1) {
        switch (_context7.prev = _context7.next) {
          case 0:
            return _context7.abrupt("return", request("".concat(CPAAS_PLATFORM, "/v1/database-tenant"), {
              method: 'DELETE',
              body: params
            }));

          case 1:
          case "end":
            return _context7.stop();
        }
      }
    }, _callee7);
  }));
  return _handleDeleteTenant.apply(this, arguments);
}